package com.learn.temp.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class Lottery {
    /**
     * 根基输入的彩票组数,输出对应生成的彩票信息
     *
     * @param groupnum
     */
    public void generateLottery(int groupnum) {
        StringBuilder sbuiler = new StringBuilder();
        for (int i = 0; i < groupnum; i++) {
            List<String> redNums = getRedNumbers();
            String blueNums = getBlueNumber();
            for (int m = 0; m < redNums.size(); m++) {
                sbuiler.append(redNums.get(m));
            }
            sbuiler.append("  ");
            sbuiler.append(blueNums);
            sbuiler.append("\n");
        }
        System.out.println(sbuiler);
    }

    /**
     * 获取双色球红球规则,33选6
     *
     * @return
     */
    public List<String> getRedNumbers() {
        return getNumber(33, 6);
    }

    /**
     * 获取双色球蓝球规则,16选1
     *
     * @return
     */
    public String getBlueNumber() {
        return getNumber(16, 1).get(0);
    }

    /**
     * 根据输入数字范围和数字数量,获取数字范围内,指定数量不重复的数据组成数字list返回
     *
     * @param numberRange 数字小范围
     * @param numberSize  数字数量
     * @return
     */
    private List<String> getNumber(int numberRange, int numberSize) {
        List<String> list = new ArrayList<>();
        String lotteryNumber = "";
        for (int i = 1; i <= numberRange; i++) {
            if (i < 10) {
                list.add("0" + i);
            } else {
                list.add(String.valueOf(i));
            }
        }
        List<String> lotteryList = new ArrayList<>();
        int roundIndex = 0;
        for (int j = 0; j < numberSize; j++) {
            int amouth = list.size();
            Random r = new Random();
            roundIndex = r.nextInt(amouth);
            lotteryNumber = list.get(roundIndex) + " ";
            lotteryList.add(lotteryNumber);
            list.remove(roundIndex);
        }
        Collections.sort(lotteryList);
        return lotteryList;
    }
}